package com.yuerlive.rainbow.widget.game;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.RainbowApp;
import com.yuerlive.rainbow.entity.Poker;
import com.yuerlive.rainbow.utils.FormatUtil;
import com.yuerlive.rainbow.utils.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by xiaoyi on 2017/5/27.
 */

public class NiuniuLayout extends RelativeLayout {
    List<Poker> pokers = new ArrayList<>();
    List<PokerView> pokerViews = new ArrayList<>();
    FrameLayout pokerContent;
    ImageView icon;
    TextView tvName;
    TextView tvTimes;

    View coverView;

    TextView tvTotal;
    TextView tvMyDeal;
    ImageView ivMyDeal;
    ImageView ivResult;

    int iconRes;
    String name;
    float times;

    public List<int[]> pokerLocations = new ArrayList<>();
    private ScaleAnimation anim = (ScaleAnimation) AnimationUtils.loadAnimation(getContext(), R.anim.niuniu_scale);

    public NiuniuLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public NiuniuLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public float getTimes() {
        return times;
    }

    public void setTimes(float times) {
        this.times = times;
        if (tvTimes!=null)
        tvTimes.setText("x"+times+"倍");
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.NiuniuLayout);
        iconRes = array.getResourceId(R.styleable.NiuniuLayout_icon, R.drawable.niuniu_qiqi);
        name = array.getString(R.styleable.NiuniuLayout_name);
        if (times==0)
        times = array.getFloat(R.styleable.NiuniuLayout_times, 2.8f);
        array.recycle();

        View.inflate(getContext(), R.layout.widget_niuniu, this);
        pokerContent = (FrameLayout) findViewById(R.id.pokerContent);
        icon = (ImageView) findViewById(R.id.ivIcon);
        icon.setImageResource(iconRes);

        tvName = (TextView) findViewById(R.id.tvName);
        tvName.setText(name);

        tvMyDeal = (TextView) findViewById(R.id.tvMyDeal);
        ivMyDeal = (ImageView) findViewById(R.id.ivMyDeal);
        tvTotal = (TextView) findViewById(R.id.tvTotal);

        tvTimes = (TextView) findViewById(R.id.tvTimes);

        coverView = findViewById(R.id.coverView);
        ivResult = (ImageView) findViewById(R.id.ivResult);
        pokerViews.clear();
        for (int i = 0; i < pokerContent.getChildCount() - 1; i++) {
            pokerViews.add((PokerView) pokerContent.getChildAt(i));
        }

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                for (int i = 0; i < pokerContent.getChildCount() - 1; i++) {
                    View poker = pokerContent.getChildAt(i);
                    int[] location = new int[2];
                    poker.getLocationOnScreen(location);
                    pokerLocations.add(location);
                }
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

        hide();


    }

    public void showBack() {
        for (PokerView pokerView : pokerViews) {
            pokerView.setVisibility(VISIBLE);
            pokerView.showBlack();
        }
    }

    public void hide() {
        for (PokerView pokerView : pokerViews) {
            pokerView.setVisibility(INVISIBLE);
        }
    }

    public void setPokers(List<Poker> pokers) {
        this.pokers.clear();
        this.pokers = pokers;
        for (int i = 0; i < pokerViews.size(); i++) {
            pokerViews.get(i).setPokerValue(pokers.get(i));
            pokerViews.get(i).show();
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        anim.setDuration(500);
        anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(Integer.MAX_VALUE);
        icon.startAnimation(anim);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        anim.cancel();
        anim = null;
    }

    public void doDeal(int deal) {
        if (deal <= 0) {
            deal = 0;
            ivMyDeal.setVisibility(VISIBLE);
            tvMyDeal.setVisibility(VISIBLE);
        } else {
            ivMyDeal.setVisibility(VISIBLE);
            tvMyDeal.setVisibility(VISIBLE);
        }
        tvMyDeal.setText(FormatUtil.formatDealTotal(deal));
        L.e("NIUNIU","deal  " + tvMyDeal.getText().toString());
    }

    private int total = 0;

    public void setDealTotal(int total) {
        if (total <= this.total && total != 0) return;
        tvTotal.setText(FormatUtil.formatDealTotal(total));
        this.total = total;
    }

    public void showCover(boolean show) {
        coverView.setVisibility(show ? VISIBLE : INVISIBLE);
    }

    public void setResult(int result) {
        String resEnd = "0";
        if (result < 10) {
            resEnd = String.valueOf(result);
        }
        switch (result) {
            case 10:
                resEnd = "nn";
                break;
            case 11:
                resEnd = "yn";
                break;
            case 12:
                resEnd = "jn";
                break;
            case 13:
                resEnd = "zd";
                break;
            case 14:
                resEnd = "wxn";
                break;
        }
        int resId = getResources().getIdentifier("niuniu_" + resEnd, "drawable", RainbowApp.getContext().getPackageName());
        ivResult.setImageResource(resId);
        ivResult.setVisibility(VISIBLE);
    }

    public void clean() {
        ivResult.setVisibility(GONE);
        coverView.setVisibility(INVISIBLE);
        hide();
        doDeal(0);
        setDealTotal(0);
    }


}
